﻿package morn.core.managers
{
    import flash.events.*;
    import morn.core.handlers.*;

    public class LoaderManager extends EventDispatcher
    {
        private var _resInfos:Array;
        private var _resLoader:ResLoader;
        private var _isLoading:Boolean;
        private var _failRes:Object;
        private var _retryNum:int = 5;
        private var _ioErrorRetry:int = 1;

        public function LoaderManager()
        {
            this._resInfos = [];
            this._resLoader = new ResLoader();
            this._failRes = {};
            return;
        }// end function

        public function load(param1:String, param2:uint, param3:Handler = null, param4:Handler = null, param5:Handler = null, param6:Boolean = true) : void
        {
            var _loc_7:* = new ResInfo2();
            new ResInfo2().url = param1;
            _loc_7.type = param2;
            _loc_7.complete = param3;
            _loc_7.progress = param4;
            _loc_7.error = param5;
            _loc_7.isCacheContent = param6;
            var _loc_8:* = ResLoader.getResLoaded(_loc_7.url);
            if (ResLoader.getResLoaded(_loc_7.url) != null)
            {
                this.endLoad(_loc_7, _loc_8, 0);
            }
            else
            {
                this._resInfos.push(_loc_7);
                this.checkNext();
            }
            return;
        }// end function

        private function checkNext() : void
        {
            var _loc_1:ResInfo2 = null;
            var _loc_2:* = undefined;
            if (this._isLoading)
            {
                return;
            }
            this._isLoading = true;
            while (this._resInfos.length > 0)
            {
                
                _loc_1 = this._resInfos.shift();
                _loc_2 = ResLoader.getResLoaded(_loc_1.url);
                if (_loc_2 != null)
                {
                    this.endLoad(_loc_1, _loc_2, 0);
                    continue;
                }
                this._resLoader.load(_loc_1.url, _loc_1.type, new Handler(this.loadComplete, [_loc_1]), _loc_1.progress, _loc_1.isCacheContent, 10000, this._failRes[_loc_1.url]);
                return;
            }
            this._isLoading = false;
            if (hasEventListener(Event.COMPLETE))
            {
                dispatchEvent(new Event(Event.COMPLETE));
            }
            return;
        }// end function

        private function loadComplete(param1:ResInfo2, param2, param3:int) : void
        {
            this.endLoad(param1, param2, param3);
            this._isLoading = false;
            this.checkNext();
            return;
        }// end function

        private function endLoad(param1:ResInfo2, param2, param3:int) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            if (param2 == null)
            {
                _loc_4 = this._failRes[param1.url] || 0;
                _loc_5 = param3 == 2 ? (this._ioErrorRetry) : (this._retryNum);
                if (_loc_4 < _loc_5)
                {
                    this._failRes[param1.url] = _loc_4 + 1;
                    this._resInfos.push(param1);
                    return;
                }
                if (param1.error != null)
                {
                    param1.error.executeWith([param1.url]);
                }
            }
            if (param1.complete != null)
            {
                param1.complete.executeWith([param2]);
            }
            return;
        }// end function

        public function loadSWF(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.SWF, param2, param3, param4, param5);
            return;
        }// end function

        public function loadMSWF(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.MSWF, param2, param3, param4, param5);
            return;
        }// end function

        public function loadBMD(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.BMD, param2, param3, param4, param5);
            return;
        }// end function

        public function loadAMF(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.AMF, param2, param3, param4, param5);
            return;
        }// end function

        public function loadTXT(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.TXT, param2, param3, param4, param5);
            return;
        }// end function

        public function loadDB(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.DB, param2, param3, param4, param5);
            return;
        }// end function

        public function loadBYTE(param1:String, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            this.load(param1, ResLoader.BYTE, param2, param3, param4, param5);
            return;
        }// end function

        public function getResLoaded(param1:String)
        {
            return ResLoader.getResLoaded(param1);
        }// end function

        public function clearResLoaded(param1:String) : void
        {
            ResLoader.clearResLoaded(param1);
            return;
        }// end function

        public function tryToCloseLoad(param1:String) : void
        {
            if (this._resLoader.url == param1)
            {
                this._resLoader.tryToCloseLoad();
                this._isLoading = false;
                this.checkNext();
            }
            return;
        }// end function

        public function get retryNum() : int
        {
            return this._retryNum;
        }// end function

        public function set retryNum(param1:int) : void
        {
            this._retryNum = param1;
            return;
        }// end function

        public function loadAssets(param1:Array, param2:Handler = null, param3:Handler = null, param4:Handler = null, param5:Boolean = true) : void
        {
            var itemCount:int;
            var itemloaded:int;
            var totalSize:int;
            var totalLoaded:int;
            var loadAssetsComplete:Function;
            var loadAssetsProgress:Function;
            var item:Object;
            var arr:* = param1;
            var complete:* = param2;
            var progress:* = param3;
            var error:* = param4;
            var isCacheContent:* = param5;
            loadAssetsComplete = function (param1:int, param2) : void
            {
                var _loc_4:* = itemloaded + 1;
                itemloaded = _loc_4;
                totalLoaded = totalLoaded + param1;
                if (itemloaded == itemCount)
                {
                    if (complete != null)
                    {
                        complete.execute();
                    }
                }
                return;
            }// end function
            ;
            loadAssetsProgress = function (param1:int, param2:Number) : void
            {
                if (progress != null)
                {
                    param2 = (totalLoaded + param1 * param2) / totalSize;
                    progress.executeWith([param2]);
                }
                return;
            }// end function
            ;
            itemCount = arr.length;
            itemloaded;
            totalSize;
            totalLoaded;
            var i:int;
            while (i < itemCount)
            {
                
                item = arr[i];
                if (item is String)
                {
                    item;
                }
                totalSize = totalSize + item.size;
                this.load(item.url, item.type, new Handler(loadAssetsComplete, [item.size]), new Handler(loadAssetsProgress, [item.size]), error, isCacheContent);
                i = (i + 1);
            }
            return;
        }// end function

    }
}

class ResInfo2 extends Object
{
    public var url:String;
    public var type:int;
    public var complete:Handler;
    public var progress:Handler;
    public var error:Handler;
    public var isCacheContent:Boolean;

    function ResInfo2()
    {
        return;
    }// end function

}

